package org.netbeans.modules.nbexpose;

import java.io.Serializable;
import java.util.Comparator;
import java.util.Date;
import javax.swing.table.TableModel;
import org.openide.ErrorManager;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;

/**
 * Top component which displays something.
 */
final class ExposeTopComponent extends TopComponent {
    
    private static ExposeTopComponent instance;
    /** path to the icon used by the component and its open action */
//    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";
    
    private static final String PREFERRED_ID = "ExposeTopComponent";

    static String ICON_PATH="icon16.png";
    
    public ExposeTableModel model;
    public TableMouseAdapter mia;
    
    private ExposeTopComponent() {
        initComponents();
        jTable1.setDefaultRenderer( TopComponent.class, new IconTableCellRenderer() );
        jTable1.setDefaultRenderer( Date.class, new DateCellRenderer() );
        
        setName(NbBundle.getMessage(ExposeTopComponent.class, "CTL_ExposeTopComponent"));
        setToolTipText(NbBundle.getMessage(ExposeTopComponent.class, "HINT_ExposeTopComponent"));
//        setIcon(Utilities.loadImage(ICON_PATH, true));
        
        model= new ExposeTableModel( this );
        TableSorter sorter= new TableSorter( model );
        
        sorter.setColumnComparator( TopComponent.class, new Comparator() {
            public int compare(Object o1, Object o2) {
                String s1= Util.getLabelFor((TopComponent)o1).toLowerCase();
                String s2= Util.getLabelFor((TopComponent)o2).toLowerCase();
                return s1.compareTo( s2 );
            }
        } );
                
        jTable1.setModel( sorter );
        sorter.setTableHeader( jTable1.getTableHeader() );

        jTable1.getColumnModel().getColumn(0).setMaxWidth(20);
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(20);

        mia= new TableMouseAdapter();
        jTable1.addMouseListener( mia );
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        closeSelectedButton = new javax.swing.JButton();
        openSelectedButton = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/netbeans/modules/nbexpose/Bundle"); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(closeSelectedButton, bundle.getString("Close_Selected")); // NOI18N
        closeSelectedButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeSelectedButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(openSelectedButton, bundle.getString("Open_Selected")); // NOI18N
        openSelectedButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openSelectedButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 521, Short.MAX_VALUE)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(269, 269, 269)
                .add(openSelectedButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(closeSelectedButton))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(closeSelectedButton)
                    .add(openSelectedButton)))
        );
    }// </editor-fold>//GEN-END:initComponents

private void closeSelectedButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeSelectedButtonActionPerformed
    model.actionsProvider.closeSelected();
}//GEN-LAST:event_closeSelectedButtonActionPerformed

private void openSelectedButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openSelectedButtonActionPerformed
    model.actionsProvider.dontCloseSelected();
}//GEN-LAST:event_openSelectedButtonActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeSelectedButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton openSelectedButton;
    // End of variables declaration//GEN-END:variables
    
    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link findInstance}.
     */
    public static synchronized ExposeTopComponent getDefault() {
        if (instance == null) {
            instance = new ExposeTopComponent();
        }
        return instance;
    }
    
    /**
     * Obtain the ExposeTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized ExposeTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            ErrorManager.getDefault().log(ErrorManager.WARNING, "Cannot find Expose component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof ExposeTopComponent) {
            return (ExposeTopComponent)win;
        }
        ErrorManager.getDefault().log(ErrorManager.WARNING, "There seem to be multiple components with the '" + PREFERRED_ID + "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }
    
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ALWAYS;
    }
    
    public void componentOpened() {
        // TODO add custom code on component opening
    }
    
    public void componentClosed() {
        // TODO add custom code on component closing
    }
    
    /** replaces this in object stream */
    public Object writeReplace() {
        return new ResolvableHelper();
    }
    
    protected String preferredID() {
        return PREFERRED_ID;
    }
    
    final static class ResolvableHelper implements Serializable {
        private static final long serialVersionUID = 1L;
        public Object readResolve() {
            return ExposeTopComponent.getDefault();
        }
    }
    
}
